<?php
/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: TANGYIN 2020-06-06 $
 *
 */

namespace app\admin\controller;

use think\facade\View;
use think\facade\Db;

use app\common\model\EmptyController as EmptyControllerModel;
use app\common\model\Example as ExampleModel;
use app\common\model\ExampleGroup as ExampleGroupModel;
use app\common\model\Fields as FieldsModel;
use app\admin\validate\Users as validateUsers;

class ExampleGroup extends Base
{
    protected $tablename;//表名
    protected $mod;//模型
    protected $moduleid;//模型id

    // 初始化
    protected function initialize()
    {
        $tablename = 'ExampleGroup';
        $mod = DB::name('module')->where('name',$tablename)->find();
        $moduleid = $mod['id'];

        $this ->tablename = $tablename;
        $this ->mod = $mod;
        $this ->moduleid = $moduleid;

        View::assign('tablename',$tablename);
        View::assign('module',$tablename);
        View::assign('mod',$mod);
        View::assign('moduleid',$moduleid);
    }

    function list(){
        //搜索
        $map = [];
        $param = input("param.");
        if(isset($param['title']) && $param['title'] != ''){
            $map[] = ['title','like','%'.$param['title'].'%'];
        }

        $siteid = $this->admin_siteid;//站点id
        $tablename = $this->tablename;//表名
        $limit = 10;

        $res = DB::name($tablename)
            ->where($map)
            ->order('orderid asc,id desc')->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ])->each(function($item, $key){
                $ExampleModel = new ExampleModel();
                $e_res = $ExampleModel -> where("id","in",$item['example_ids'])->select();
                $ex_str = '';
                foreach($e_res as $k=>$v){
                    $ex_str .= $v['title']."(".$v['name']."),";
                }
                $item['example_ids_name'] = trim($ex_str,",");
                $item['add_time'] = date("Y-m-d",$item['add_time']);
                return $item;
            });
        $page = $res->render();
        $total = $res->total();

        View::assign('total', $total);
        View::assign('page', $page);
        View::assign('res', $res);

        //字段遍历(表格显示is_table=1)
//        $FieldsModel = new FieldsModel();
//        $fields_res = $FieldsModel ->getModuleFields($this->moduleid);
//        View::assign('fields_res', $fields_res);

        return View::fetch();
    }

    public function add(){
        $param = input('param.');
        if(IS_POST){
            $ExampleGroupModel = new ExampleGroupModel();
            $param['act'] = 'add';
            $res = $ExampleGroupModel -> handle($param);
            return dyajaxReturn($res['code'], $res['msg']);

        }else {
            //示例表遍历
            $ExampleModel = new ExampleModel();
            $exa_res = $ExampleModel -> where("status","=",1)
                ->select();

            View::assign('exa_res', $exa_res);
            return View::fetch();
        }
    }


    public function edit(){
        $param = input('param.');
        $ExampleGroupModel = new ExampleGroupModel();
        if(IS_POST){

            $param['act'] = 'edit';
            $res = $ExampleGroupModel->handle($param);

            return dyajaxReturn($res['code'], $res['msg']);
        }else{
            $id = $param['id'];
            $info = $ExampleGroupModel->where('id','=',$id)->find();

            //示例表遍历
            $ExampleModel = new ExampleModel();
            $exa_res = $ExampleModel -> where("status","=",1)
                ->select();

            View::assign('exa_res', $exa_res);
            View::assign('info',$info);
            View::assign('id',$id);

            return View::fetch();
        }
    }

    //删除
    public function delete(){
        $param = input("param.");
        $table = $param['table'];
        $ids = $param['ids'];

        $res = DB::name($table)->where("id","in",$ids)->delete();
        if(!$res){
            return dyajaxReturn(0, '删除失败');
        }
        adminLog($table.'表的删除',$ids);
        return dyajaxReturn(1, '删除成功');
    }


    //状态改变
    public function changeStatus(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];
        $field = $param['field'];
        $checked = $param['checked'];

        if($checked == 'true'){
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>1]);
        }else{
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>2]);
        }
        if(!$res){
            return dyajaxReturn(0,'操作失败');
        }
        return dyajaxReturn(1,'操作成功');
    }


}